<?php
/**
 * common repository file Created by PhpStorm.
 * User: wumengmeng
 * Date: 2019/08/20
 * Time: 06:07
 */
 
 namespace HiCommon\Repository;

 use HiCommon\Model\CouponGrantDetail;
 use HiCommon\Model\ViewShopCouponGrantDetail;

class CouponGrantDetailRepository extends BaseRepository
{
    public function base_model(){
        return new \HiCommon\Model\CouponGrantDetail();
    }

    public function get_all_view_to_shop( array $arr_search = [] , array $arr_option = []  ){
        return ViewShopCouponGrantDetail::where(
            function ($query)
            use($arr_search) {
                if( !empty( $arr_search['pid'] )  ){
                    $query->where('pid',$arr_search['pid']);
                }
                if( !empty( $arr_search['type'] ) ){
                    if( $arr_search['type'] == 1 ){   //最新
                        $time = date( "Y-m-d H:i:s" , time()-3600*24*3 );
                        $query->where('created_at','>' , $time );
                        $query->where('status',1 );
                    }else if( $arr_search['type'] == 2 ){  //即将到期
                        $time = time() + 3600*24*3;
                        $query->where('over_at','<' , $time );   //过期时间<3天后的时间
                        $query->where('status',1 );  //状态为未使用
                    }else if( $arr_search['type'] == 3 ){  //已过期
                        $query->where('status',3 );
                    }else if( $arr_search['type'] == 4 ){  //已使用
                        $query->where('status',2 );
                    }
                }
                if( !empty( $arr_search['arr_ids'] ) ){
                    $query->whereIn( 'shop_id' ,$arr_search['arr_ids']);
                }
                //商户端app
                if( !empty( $arr_search['times'] ) ){
                    $times = explode( ' - ', $arr_search['times'] );
                    $query->where( 'updated_at' , '>' ,$times[0]);
                    $query->where( 'updated_at' , '<' ,$times[1]);
                }
            })->commonget( $arr_option );


    }

    /**
     * 分页获取全部数据
     *
     * @param array $arr_option
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_all_view( array $arr_search = [] ,$arr_option = [])
    {
        return ViewShopCouponGrantDetail::groupBy('shop_id')->where(
            function ($query)
            use($arr_search) {
                //小程序需要的搜索
                if( !empty( $arr_search['pid'] )  ){
                    $query->where('pid',$arr_search['pid']);
                }
                if( !empty( $arr_search['type'] ) ){
                    if( $arr_search['type'] == 1 ){   //最新
                        $time = date( "Y-m-d H:i:s" , time()-3600*24*3 );
                        $query->where('created_at','>' , $time );
                        $query->where('status',1 );
                    }else if( $arr_search['type'] == 2 ){  //即将到期
                        $time = time() + 3600*24*3;
                        $query->where('over_at','<' , $time );   //过期时间<3天后的时间
                        $query->where('status',1 );  //状态为未使用
                    }else if( $arr_search['type'] == 3 ){  //已过期
                        $query->where('status',3 );
                    }else if( $arr_search['type'] == 4 ){  //已使用
                        $query->where('status',2 );
                    }
                }
            })->commonpage($arr_option);
    }



    /**
     * 分页获取全部数据
     *
     * @param array $arr_option
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_all($arr_option = [])
    {
        return CouponGrantDetail::commonget($arr_option);
    }


    /**
     *  获取除洗车券的所有有效优惠券
     *  hinq 2019 09 21
     *
     **/
    public function get_all_to_goods( array $arr_option = [])
    {
        return CouponGrantDetail::whereIn('pid',[2,3])->commonget($arr_option);
    }


    /**
     *  定时任务专用【其他地方谨慎调用】
     *  hinq 2019 09 23
     *
     **/
    public function get_timed_task_list(){
        $arr_option['field'] = ['id'];
        return CouponGrantDetail::where('over_at' , '<' , time() )->where(['status'=>1])->commonget( $arr_option );
    } 


    /**
     *  定时任务专用【其他地方谨慎调用】
     *  hinq 2019 09 23
     *
     **/
    public function edit_timed_task_list( array $ids = [] ){
        $data['status'] = 3;
        return CouponGrantDetail::whereIn('id',$ids)->update( $data );
    }


    /**
     * 分页获取数据
     *
     * @param array $arr_option
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_list(array $arr_search = [] , array $arr_option = [] )
    {
        return CouponGrantDetail::where(
            function ($query)
            use($arr_search) {
                //管理后台需要的搜索
                if( !empty( $arr_search['name'] ) && !empty( $arr_search['user_id'] ) ){
                    $query->where('user_id',$arr_search['user_id']);
                }
                if( !empty( $arr_search['shop_name'] ) && !empty( $arr_search['repairstore_id'] ) ){
                    $query->where('repairstore_id',$arr_search['repairstore_id']);
                }
                if( !empty( $arr_search['status'] )  ){
                    $query->where('status',$arr_search['status']);
                }
                if( !empty( $arr_search['ticket_num'] )  ){
                    $query->where('ticket_num','like','%'.$arr_search['ticket_num'].'%');
                }
                //小程序需要的搜索
                if( !empty( $arr_search['pid'] )  ){
                    $query->where('pid',$arr_search['pid']);
                }

                if( !empty( $arr_search['type'] ) ){
                    if( $arr_search['type'] == 1 ){   //最新
                        $time = date( "Y-m-d H:i:s" , time()-3600*24*3 );
                        $query->where('created_at','>' , $time );
                        $query->where('status',1 );
                    }else if( $arr_search['type'] == 2 ){  //即将到期
                        $time = time() + 3600*24*3;
                        $query->where('over_at','<' , $time );   //过期时间<3天后的时间
                        $query->where('status',1 );  //状态为未使用
                    }else if( $arr_search['type'] == 3 ){  //已过期
                        $query->where('status',3 ); 
                    }else if( $arr_search['type'] == 4 ){  //已使用
                        $query->where('status',2 ); 
                    }
                }
                if( !empty( $arr_search['arr_ids'] ) ){
                    $query->whereIn( 'id' ,$arr_search['arr_ids']);
                }
                //商户端app
                if( !empty( $arr_search['times'] ) ){
                    $times = explode( ' - ', $arr_search['times'] );
                    $times1 = strtotime( $times[0] );
                    $times2 = strtotime( $times[1] );
                    $query->where( 'updated_at' , '>' , date( 'Y-m-d' ,$times1 ) . ' 00:00:00');
                    $query->where( 'updated_at' , '<' ,date( 'Y-m-d' ,$times2 ) . ' 23:5959');
                }
            })->commonpage($arr_option);
    }

    

    // public function get_valid_coupon( array $arr_input ){

    //     return CouponGrantDetail::commonget($arr_option);



    // }




    
    /**
     * 通过where分页获取数据
     *
     * @param array $arr_where
     * @param array $arr_option
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_list_by_where($arr_where,$arr_option = [])
    {
        return CouponGrantDetail::where($arr_where)->commonpage($arr_option);

    }
    
    /**
     * 根据条件统计数量
     *
     * @param array $arr_where
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_count_by_where($arr_where)
    {
        return CouponGrantDetail::where($arr_where)->count();
    }
    
    /**
     * 根据条件统计总和
     *
     * @param array  $arr_where
     * @param string $s_field
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_sum_by_where($arr_where = [],$s_field = 'mark')
    {
        return CouponGrantDetail::where($arr_where)->sum($s_field);
    }

    /**
     * 添加一条数据
     *
     * @param array $arr_input
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function add_one($arr_input)
    {
        return CouponGrantDetail::create($arr_input);
    }

    /**
     * 查询一条数据
     *
     * @param int $n_id
     * @param array $arr_option
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_one(int $n_id, $arr_option = [])
    {
        return CouponGrantDetail::commonfind($n_id, $arr_option);
    }

    /**
     * 根据where条件查询一条数据
     *
     * @param array $where
     * @param array $arr_option
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_one_where( array $arr_where, $arr_option = [])
    {
        return CouponGrantDetail::where( $arr_where)->commonFirst( $arr_option);
    }



    /**
     * 修改一条数据
     *
     * @param array $arr_input
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function edit_one(array $arr_input)
    {
        return CouponGrantDetail::where('id',$arr_input['id'])->update($arr_input);
    }

    /**
     * 删除一条数据
     *
     * @param int $n_id
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function delete_one(int $n_id)
    {
        return CouponGrantDetail::where('id', $n_id)
                             ->commondelete();
    }

    /**
     * 修改一条数据状态
     *
     * @param int $n_id
     * @param int $n_state
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function change_one_state(int $n_id,int $n_state)
    {
        return CouponGrantDetail::where('id',$n_id)->update(['status'=>$n_state]);
    }
    
    /**
     * 字段自增
     *
     * @param int    $n_id
     * @param string $s_field
     * @param int    $n_step
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function increase_num($n_id,$s_field = 'commend_num',$n_step = 1)
    {
        return CouponGrantDetail::where('id',$n_id)->increment($s_field,$n_step);
    }

    /**
     * 字段自减
     *
     * @param int    $n_id
     * @param string $s_field
     * @param int    $n_step
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function decrease_num($n_id,$s_field = 'commend_num',$n_step = 1)
    {
        return CouponGrantDetail::where('id',$n_id)->decrement($s_field,$n_step);
    }


}